import java.util.ArrayList;
import java.util.List;

public class test4 {

        public static void main(String[] args) {
            int max = 20000;
            List<Integer> primes = sieveOfEratosthenes(max);

            int count = 0;
            for (int prime : primes) {
                System.out.print(prime + "\t");
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }

            if (count % 5 != 0) {
                System.out.println();
            }
        }

        public static List<Integer> sieveOfEratosthenes(int max) {
            boolean[] isPrime = new boolean[max + 1];
            for (int i = 2; i <= max; i++) {
                isPrime[i] = true;
            }

            for (int factor = 2; factor * factor <= max; factor++) {
                if (isPrime[factor]) {
                    for (int multiple = factor * factor; multiple <= max; multiple += factor) {
                        isPrime[multiple] = false;
                    }
                }
            }

            List<Integer> primes = new ArrayList<>();
            for (int i = 2; i <= max; i++) {
                if (isPrime[i]) {
                    primes.add(i);
                }
            }

            return primes;
        }
}
